package com.kyxx.topics.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.bean.MessageCommentBean;
import com.kyxx.topics.net.OkGoRequest;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.Constants;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.request.BaseRequest;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

public class MessageNotifyFragment extends Fragment {
    private RelativeLayout rl_progress;
    private ProgressBar avi;
    private LinearLayout ll_reload;
    private LinearLayout ll_no;
    private SwipeMenuListView smlv;
    private int pageSize = 1;
    private int countSize = 10;
    private int lastVisiblePosition;
    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    rl_progress.setVisibility(View.GONE);
                    break;
                case 2:
                    rl_progress.setVisibility(View.VISIBLE);
                    avi.setVisibility(View.GONE);
                    ll_no.setVisibility(View.GONE);
                    ll_reload.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_message_notify, container, false);
        rl_progress = (RelativeLayout) view.findViewById(R.id.rl_progress);
        avi = (ProgressBar) view.findViewById(R.id.avi);
        ll_no = (LinearLayout) view.findViewById(R.id.ll_no);
        ll_reload = (LinearLayout) view.findViewById(R.id.ll_reload);
        ll_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pageSize = 1;
                getData(Constants.INITDATA);
            }
        });
        smlv = (SwipeMenuListView) view.findViewById(R.id.smlv);
        smlv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if (lastVisiblePosition == smlv.getLastVisiblePosition())
                    return;
                lastVisiblePosition = smlv.getLastVisiblePosition();
                Log.e("topic", lastVisiblePosition + "onScroll" + smlv.getCount());
                if (lastVisiblePosition == smlv.getCount() - 2 && smlv.getCount() >= countSize) {
                    pageSize++;
                    getData(Constants.LOADMORE);
                }
            }
        });

        return view;
    }

    private void getData(final int type) {
        HttpParams params = new HttpParams();
        params.put("uid", BaseApp.getModel().getId());
        params.put("p", pageSize);
        params.put("rows", countSize);
        String cacheKey = "notify&uid=" + BaseApp.getModel().getId() + "p=" + pageSize + "&rows=" + countSize;
        OkGoRequest.post(UrlUtils.NOTIFY, getActivity(), cacheKey, CacheMode.REQUEST_FAILED_READ_CACHE, params, new StringCallback() {
            @Override
            public void onBefore(BaseRequest request) {
                super.onBefore(request);
                rl_progress.setVisibility(View.VISIBLE);
                avi.setVisibility(View.VISIBLE);
                ll_reload.setVisibility(View.GONE);
            }

            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onCacheError(Call call, Exception e) {
                super.onCacheError(call, e);
                handler.sendEmptyMessageDelayed(2, Constants.DELAYTIMES);
            }

            @Override
            public void onSuccess(String s, Call call, Response response) {
                rl_progress.setVisibility(View.GONE);
                try {
                    JSONObject result = new JSONObject(s);
                    switch (type) {
                        case Constants.INITDATA:
//                            messageCommentBeanList = JSON.parseArray(result.get("data").toString(), MessageCommentBean.class);
                            break;
                        case Constants.LOADMORE:
//                            messageCommentBeanList.addAll(JSON.parseArray(result.get("data").toString(), MessageCommentBean.class));
                    }
//                    messageCommentAdapter.notifyDataSetChanged();
                    lastVisiblePosition = smlv.getLastVisiblePosition();
//                    if (messageCommentBeanList==null||messageCommentBeanList.size() == 0){
//                        rl_progress.setVisibility(View.VISIBLE);
//                        ll_no.setVisibility(View.VISIBLE);
//                        ll_reload.setVisibility(View.GONE);
//                        avi.setVisibility(View.GONE);
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    rl_progress.setVisibility(View.VISIBLE);
                    ll_no.setVisibility(View.VISIBLE);
                    ll_reload.setVisibility(View.GONE);
                    avi.setVisibility(View.GONE);
                }
            }
        });
    }

    class MessageNotifyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            return null;
        }
    }

}
